package com.ywxx.dyhelper.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywxx.dyhelper.BuildConfig;
import com.ywxx.dyhelper.model.DYConfig;

import java.util.ArrayList;

/**
 * 抖音各版本ID适配获取
 */
public class DYVersionUtils
{
    public static DYConfig getTypeConfig(int type, String dyver)
    {

        if (type == 1 || type == 2)
        {
            type = 1;
        }

        if (type == 4 || type == 5 || type == 6)
        {
            type = 4;
        }

        if (type == 9)
        {
            type = 7;
        }

        ArrayList<DYConfig> dyConfigs = ConstValues.getInstance().getDyConfigs();
        for (DYConfig cfg : dyConfigs)
        {
            if (cfg.type == type)
            {
                return cfg;
            }

        }
        return new DYConfig();
    }


    private static DYConfig getCancelID(String dyver)
    {
        DYConfig obj = new DYConfig();
        if (dyver.equals("11.6.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("zs");//按钮
            obj.id_b = ConstValues.getdypackagenameid("gzg");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("et7");//recyleview
        }
        if (dyver.equals("12.4.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("a2n");//按钮
            obj.id_b = ConstValues.getdypackagenameid("hr_");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("fd1");////recyleview
        }
        if (dyver.equals("12.5.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("a2y");//按钮
            obj.id_b = ConstValues.getdypackagenameid("hu3");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("ff_");////recyleview
        }
        return obj;
    }

    /**
     * 通用引流模式
     * @param dyver
     * @return
     */
    private static DYConfig getCommonGuide(String dyver)
    {
        DYConfig obj = new DYConfig();
        if (dyver.equals("11.6.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("bdi");//关注
            obj.id_b = ConstValues.getdypackagenameid("ar2");//点赞
            obj.id_c = ConstValues.getdypackagenameid("a91");//广告
            obj.id_d = ConstValues.getdypackagenameid("gjr");//直播标识
        }
        if (dyver.equals("12.4.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("blb");//关注
            obj.id_b = ConstValues.getdypackagenameid("avf");//点赞
            obj.id_c = ConstValues.getdypackagenameid("a91");//广告
            obj.id_d = ConstValues.getdypackagenameid("dav");//直播标识
        }
        if (dyver.equals("12.5.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("bmk");//关注 buttonID
            obj.id_b = ConstValues.getdypackagenameid("aw7");//点赞 awa->    aw7 按钮ID
            obj.id_c = ConstValues.getdypackagenameid("a91");//详情内容
            obj.id_d = ConstValues.getdypackagenameid("dg9");//直播标识
        }
        return obj;
    }

    /**
     * 精准引流
     * @param dyver
     * @return
     */
    private static DYConfig getAccurateGuide(String dyver)
    {
        DYConfig obj = new DYConfig();
        if (dyver.equals("11.6.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("zs");//头像
            obj.id_b = ConstValues.getdypackagenameid("gzg");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("apn");//获赞个数
            obj.id_d = ConstValues.getdypackagenameid("be_");//关注个数
            obj.id_e = ConstValues.getdypackagenameid("be5");//粉丝个数
            obj.id_f = ConstValues.getdypackagenameid("gdp");//作品日期
            obj.id_g = ConstValues.getdypackagenameid("aqp");//点赞按钮
            obj.id_h = ConstValues.getdypackagenameid("ctg");//作品点赞数
            obj.id_i = ConstValues.getdypackagenameid("kr");//返回按钮
            obj.id_j = ConstValues.getdypackagenameid("et7");//粉丝列表recyleview
            obj.id_k = ConstValues.getdypackagenameid("cvp");//recyleview
            obj.id_l = ConstValues.getdypackagenameid("bdl");//关注按钮

        }

        if (dyver.equals("12.4.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("d3x");//头像
            obj.id_b = ConstValues.getdypackagenameid("hr_");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("aub");//获赞个数
            obj.id_d = ConstValues.getdypackagenameid("bm4");//关注个数
            obj.id_e = ConstValues.getdypackagenameid("bly");//粉丝个数
            obj.id_f = ConstValues.getdypackagenameid("h3z");//作品日期
            obj.id_g = ConstValues.getdypackagenameid("avf");//点赞按钮
            obj.id_h = ConstValues.getdypackagenameid("d5x");//作品点赞数
            obj.id_i = ConstValues.getdypackagenameid("l4");//返回按钮
            obj.id_j = ConstValues.getdypackagenameid("fd1");//粉丝列表recyleview
            obj.id_k = ConstValues.getdypackagenameid("d8k");//recyleview
            obj.id_l = ConstValues.getdypackagenameid("ble");//关注按钮
        }

        if (dyver.equals("12.5.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("cgq");//头像
            obj.id_b = ConstValues.getdypackagenameid("hu3");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("av4");//获赞个数
            obj.id_d = ConstValues.getdypackagenameid("bnc");//关注个数
            obj.id_e = ConstValues.getdypackagenameid("bn7");//粉丝个数
            obj.id_f = ConstValues.getdypackagenameid("h6m");//作品日期
            obj.id_g = ConstValues.getdypackagenameid("aw7");//点赞按钮
            obj.id_h = ConstValues.getdypackagenameid("d75");//作品点赞数
            obj.id_i = ConstValues.getdypackagenameid("l4");//返回按钮
            obj.id_j = ConstValues.getdypackagenameid("ff_");//粉丝列表recyleview
            obj.id_k = ConstValues.getdypackagenameid("d9s");//作品recyleview
            obj.id_l = ConstValues.getdypackagenameid("ew2");//关注按钮
        }
        return obj;
    }

    private static DYConfig AutoAdvertiseme(String dyver)
    {
        DYConfig obj = new DYConfig();
        if (dyver.equals("11.6.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("f29");//条目ID
            obj.id_b = ConstValues.getdypackagenameid("eo3");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("b0u");//表情ID 打招呼
            obj.id_d = ConstValues.getdypackagenameid("dhf");//输入框ID
            obj.id_e = ConstValues.getdypackagenameid("f1g");//发送ID
            obj.id_f = ConstValues.getdypackagenameid("a64");//表情按钮ID  c06
            obj.id_g = ConstValues.getdypackagenameid("flh");//我的收藏表情ID
            obj.id_h = ConstValues.getdypackagenameid("b0u");//表情ID
            obj.id_i = ConstValues.getdypackagenameid("csd");//返回键ID
            obj.id_j = ConstValues.getdypackagenameid("f28");//recycleview
        }
        if (dyver.equals("12.4.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("fmr");//条目ID
            obj.id_b = ConstValues.getdypackagenameid("eo5");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("b6d");//表情ID
            obj.id_d = ConstValues.getdypackagenameid("dy9");//输入框ID
            obj.id_e = ConstValues.getdypackagenameid("flu");//发送ID
            obj.id_f = ConstValues.getdypackagenameid("c9d");//表情按钮ID
            obj.id_g = ConstValues.getdypackagenameid("g94");//我的收藏表情ID
            obj.id_h = ConstValues.getdypackagenameid("b6d");//表情ID
            obj.id_i = ConstValues.getdypackagenameid("d4s");//返回键ID
            obj.id_j = ConstValues.getdypackagenameid("fmq");//recycleview
        }
        if (dyver.equals("12.5.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("fp1");//条目ID
            obj.id_b = ConstValues.getdypackagenameid("eo5");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("b77");//表情ID
            obj.id_d = ConstValues.getdypackagenameid("e01");//输入框ID
            obj.id_e = ConstValues.getdypackagenameid("fo4");//发送ID
            obj.id_f = ConstValues.getdypackagenameid("a_9");//表情按钮ID
            obj.id_g = ConstValues.getdypackagenameid("gae");//我的收藏表情ID
            obj.id_h = ConstValues.getdypackagenameid("b77");//表情ID
            obj.id_i = ConstValues.getdypackagenameid("d5z");//返回键ID
            obj.id_j = ConstValues.getdypackagenameid("fp0");//recycleview
        }
        return obj;
    }

    /**
     * 一键识别
     * @param dyver
     * @return
     */
    private static DYConfig Recognition(String dyver)
    {
        DYConfig obj = new DYConfig();
        if (dyver.equals("11.6.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("f29");//头像
            obj.id_b = ConstValues.getdypackagenameid("gzg");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("apn");//获赞个数
            obj.id_d = ConstValues.getdypackagenameid("be_");//关注个数
            obj.id_e = ConstValues.getdypackagenameid("be5");//粉丝个数
            obj.id_f = ConstValues.getdypackagenameid("p1");//头像
            obj.id_g = ConstValues.getdypackagenameid("bdl");//取消关注按钮
            obj.id_h = ConstValues.getdypackagenameid("ctg");//作品点赞数
            obj.id_i = ConstValues.getdypackagenameid("kr");//返回按钮
            obj.id_j = ConstValues.getdypackagenameid("f28");//消息列表recyleview
            obj.id_k = ConstValues.getdypackagenameid("d8k");//recyleview

            obj.id_l = ConstValues.getdypackagenameid("fsc");//更多功能按钮
            obj.id_m = ConstValues.getdypackagenameid("v2");//拉黑按钮
            obj.id_n = ConstValues.getdypackagenameid("ekp");//移除粉丝按钮
            obj.id_o = ConstValues.getdypackagenameid("eew");//对话框确认按钮
            obj.id_p = ConstValues.getdypackagenameid("eae");//对话框取消按钮
        }

        if (dyver.equals("12.4.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("fmr");//条目
            obj.id_b = ConstValues.getdypackagenameid("eo5");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("aub");//获赞个数
            obj.id_d = ConstValues.getdypackagenameid("bm4");//关注个数
            obj.id_e = ConstValues.getdypackagenameid("bly");//粉丝个数
            obj.id_f = ConstValues.getdypackagenameid("qg");//头像
            obj.id_g = ConstValues.getdypackagenameid("ble");//取消关注按钮
            obj.id_h = ConstValues.getdypackagenameid("d5x");//作品点赞数
            obj.id_i = ConstValues.getdypackagenameid("l4");//返回按钮
            obj.id_j = ConstValues.getdypackagenameid("fmq");//消息列表recyleview
            obj.id_k = ConstValues.getdypackagenameid("cvp");//recyleview

            obj.id_l = ConstValues.getdypackagenameid("gfi");//更多功能按钮
            obj.id_m = ConstValues.getdypackagenameid("wr");//拉黑按钮
            obj.id_n = ConstValues.getdypackagenameid("f4v");//移除粉丝按钮
            obj.id_o = ConstValues.getdypackagenameid("eey");//对话框确认按钮
            obj.id_p = ConstValues.getdypackagenameid("eag");//对话框取消按钮
        }

        if (dyver.equals("12.5.0"))
        {
            obj.id_a = ConstValues.getdypackagenameid("fp1");//条目
            obj.id_b = ConstValues.getdypackagenameid("eo5");//名称ID
            obj.id_c = ConstValues.getdypackagenameid("av4");//获赞个数
            obj.id_d = ConstValues.getdypackagenameid("bnc");//关注个数
            obj.id_e = ConstValues.getdypackagenameid("bn7");//粉丝个数
            obj.id_f = ConstValues.getdypackagenameid("qq");//头像 qr
            obj.id_g = ConstValues.getdypackagenameid("bmn");//关注按钮 取消关注
            obj.id_h = ConstValues.getdypackagenameid("d75");//作品点赞数
            obj.id_i = ConstValues.getdypackagenameid("l4");//返回按钮
            obj.id_j = ConstValues.getdypackagenameid("fp0");//消息列表recyleview
            obj.id_k = ConstValues.getdypackagenameid("d9s");//作品recyleview

            obj.id_l = ConstValues.getdypackagenameid("ghx");//更多功能按钮
            obj.id_m = ConstValues.getdypackagenameid("wz");//拉黑按钮
            obj.id_n = ConstValues.getdypackagenameid("f76");//移除粉丝按钮
            obj.id_o = ConstValues.getdypackagenameid("eey");//对话框确认按钮
            obj.id_p = ConstValues.getdypackagenameid("eag");//对话框取消按钮

        }
        return obj;
    }

    /**
     * 解密秘钥
     * @param dyver
     * @return
     */
    public static String getConfigkey(String dyver)
    {
        StringBuffer keys = new StringBuffer();
        keys.append(BuildConfig.APPLICATION_ID.split("\\.")[1]);
        keys.append(dyver);
        keys.append("dy");
        return EncryptUtil.getMd5Value(keys.toString()).substring(8, 24);
    }

    public static void ParseObjectConfig(String dyver, String content) throws Exception
    {

        JSONObject res = JSON.parseObject(content);
        JSONArray js = JSON.parseArray(EncryptUtil.aesDecrypt(res.getString("config"), getConfigkey(dyver)));
        ArrayList<DYConfig> objs = new ArrayList<>();
        if (js != null && js.size() > 0)
        {
            for (int n = 0; n < js.size(); n++)
            {
                JSONObject obj = js.getJSONObject(n);
                DYConfig config = new DYConfig();
                config.type = obj.getIntValue("tasktype");

                config.id_a = ConstValues.getdypackagenameid(obj.getString("a"));
                config.id_b = ConstValues.getdypackagenameid(obj.getString("b"));
                config.id_c = ConstValues.getdypackagenameid(obj.getString("c"));
                config.id_d = ConstValues.getdypackagenameid(obj.getString("d"));
                config.id_e = ConstValues.getdypackagenameid(obj.getString("e"));
                config.id_f = ConstValues.getdypackagenameid(obj.getString("f"));
                config.id_g = ConstValues.getdypackagenameid(obj.getString("g"));
                config.id_h = ConstValues.getdypackagenameid(obj.getString("h"));
                config.id_i = ConstValues.getdypackagenameid(obj.getString("i"));
                config.id_j = ConstValues.getdypackagenameid(obj.getString("j"));
                config.id_k = ConstValues.getdypackagenameid(obj.getString("k"));
                config.id_l = ConstValues.getdypackagenameid(obj.getString("l"));
                config.id_m = ConstValues.getdypackagenameid(obj.getString("m"));
                config.id_n = ConstValues.getdypackagenameid(obj.getString("n"));
                config.id_o = ConstValues.getdypackagenameid(obj.getString("o"));
                config.id_p = ConstValues.getdypackagenameid(obj.getString("p"));
                config.id_q = ConstValues.getdypackagenameid(obj.getString("q"));
                config.id_r = ConstValues.getdypackagenameid(obj.getString("r"));
                config.id_s = ConstValues.getdypackagenameid(obj.getString("s"));
                config.id_t = ConstValues.getdypackagenameid(obj.getString("t"));
                objs.add(config);
            }
        }

        ConstValues.getInstance().setDyConfigs(objs);
    }


}
